<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Internals\StudyInternal;
use App\Internals\CourseInternal;
use App\Jobs\CourseStartJob;

class WechatPushCourseBeingStartCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:wechat-push-course-being-start';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'The wechat push of course being start';

    protected $courseInfoArr = [];

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info('course-being-start start');
        try {
            $time = microtime(true);
            $page = 1;
            $limit = 100;

            // 获取所有用户当前正在学习的课程
            while($currents = StudyInternal::getAllUsersCurrent(true, $page, $limit)) {
                Log::info('currents: ' . var_export($currents, true));
                if (!count($currents)) {
                    break;
                }

                foreach($currents as $current) {
                    $userId = $current['userId'];
                    $courseId = $current['courseId'];

                    if ($current['isEnd']) {
                        // 检查用户是否已购买下一等级课程。如果有，就发送push消息
                        if ($studyNextCourse = StudyInternal::getCourseNextLevel($userId, $current['level'])) {
                            $courseId = $studyNextCourse['courseId'];
                        } else {
                            continue;
                        }
                    } else {
                        // 如果当前用户课程已推送课时为0，发送开课通知，否则不发送
                        $lessonPushed = StudyInternal::getCountPushedLesson($userId, $courseId);
                        Log::info('$lessonPushed: ' . $lessonPushed);
                        if ($lessonPushed) {
                            continue;
                        }
                    }

                    // 获取course信息
                    $courseInfo = $this->getCourseInfo($courseId);
                    Log::info('$courseInfo: ' . var_export($courseInfo, true));
                    if (! $courseInfo) {
                        continue;
                    }

                    dispatch(new CourseStartJob(
                        $userId,
                        $courseInfo['level'],
                        date('Y-m-d 20:00', strtotime('+1 day', time())),
                        $courseInfo['seriesName']
                    ));
                    Log::info('CourseStartJob: ' . var_export([
                        $userId,
                        $courseInfo['level'],
                        date('Y-m-d 20:00', strtotime('+1 day', time())),
                        $courseInfo['seriesName']
                    ], true));
                }

                $page++;
            }

            Log::info('course-being-start cost: ' . (microtime(true)-$time));
        } catch (\Exception $e) {
            var_dump($e->getMessage());
            Log::info('course-being-start failed: ' . $e->getMessage());
        }
    }

    // 获取course信息
    private function getCourseInfo($courseId)
    {
        if (isset($this->courseInfoArr[$courseId])) {
            $courseInfo = $this->courseInfoArr[$courseId];
        } else {
            $courseInfo = CourseInternal::getCourse($courseId);
            if($courseInfo) {
                $this->courseInfoArr[$courseId] = $courseInfo;
            }
        }

        return $courseInfo;
    }
}
